package com.yiren.dbaa.module.practice_set.basestation.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.module.practice_set.basestation.bean.BaseStationBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lwb
 * @date : 2022/12/12 10:17
 * @description:
 */
public class BaseStationHomeAdapter extends BaseAdapter {

    private Context context;
    private List<BaseStationBean> list;
    private OnManageClickListener listener;
    public int mSelect = 0;

    public BaseStationHomeAdapter(Context context, List<BaseStationBean> list, OnManageClickListener listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.list_basestation_home, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        BaseStationBean baseStationBean = list.get(position);
        holder.tvBaseName.setText(baseStationBean.getName());
        if (baseStationBean.getState() == 1) {
            holder.tvBaseState.setText("在线");
        } else {
            holder.tvBaseState.setText("离线");
        }
        if (baseStationBean.getCalibration() == 1) {
            holder.tvCalibrationState.setText("已标定");
        } else {
            holder.tvCalibrationState.setText("未标定");
        }

        holder.tvManage.setOnClickListener(v -> {
            if (null != listener) {
                listener.onManageClick(list.get(position));
            }
        });

        holder.tvDetails.setOnClickListener(v -> {
            if (null != listener) {
                listener.onDetailsClick(list.get(position));
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_base_name)
        TextView tvBaseName;
        @BindView(R.id.tv_base_state)
        TextView tvBaseState;
        @BindView(R.id.tv_calibration_state)
        TextView tvCalibrationState;
        @BindView(R.id.tv_details)
        TextView tvDetails;
        @BindView(R.id.tv_manage)
        TextView tvManage;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnManageClickListener {

        void onManageClick(BaseStationBean baseStationBean);

        void onDetailsClick(BaseStationBean baseStationBean);
    }

}